package com.app.utils.util.view;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

import com.app.utils.R;
import com.app.utils.util.AppUtils;


/**
 * Created by xuxinghai on 2017/4/21.
 */

public class ImageLoadingDrawable extends Drawable{
    private Paint mRingBackgroundPaint;
    private int mRingBackgroundColor;
    // 画圆环的画笔
    private Paint mRingPaint;
    // 圆环颜色
    private int mRingColor;
    // 半径
    private float mRadius;
    // 圆环半径
    private float mRingRadius;
    // 圆环宽度
    private float mStrokeWidth;
    // 圆心x坐标
    private int mXCenter;
    // 圆心y坐标
    private int mYCenter;
    // 总进度
    private int mTotalProgress = 10000;
    // 当前进度
    private int mProgress;

    public ImageLoadingDrawable(){
        initAttrs();
    }

    private void initAttrs() {
        mRadius = 50;
        mStrokeWidth = 8;
        mRingBackgroundColor = AppUtils.getInstance().getContext().getResources().getColor(R.color.common_font_color_3);
        mRingColor = AppUtils.getInstance().getContext().getResources().getColor(R.color.common_bg_color_2);
        mRingRadius = mRadius + mStrokeWidth / 2;
        initVariable();
    }

    private void initVariable() {
        mRingBackgroundPaint = new Paint();
        mRingBackgroundPaint.setAntiAlias(true);
        mRingBackgroundPaint.setColor(mRingBackgroundColor);
        mRingBackgroundPaint.setStyle(Paint.Style.FILL);
//        mRingBackgroundPaint.setStrokeWidth(mStrokeWidth);

        mRingPaint = new Paint();
        mRingPaint.setAntiAlias(true);
        mRingPaint.setColor(mRingColor);
        mRingPaint.setStyle(Paint.Style.STROKE);
        mRingPaint.setStrokeWidth(mStrokeWidth);
    }

    @Override
    public void draw(Canvas canvas) {
//        if(mProgress <= 0){
//            drawBar(canvas,mTotalProgress,mRingBackgroundPaint);
            drawBackground(canvas,mRingBackgroundPaint);
//        }

        drawBar(canvas,mProgress,mRingPaint);
    }

    private void drawBackground(Canvas canvas, Paint mRingBackgroundPaint) {
        Rect bound= getBounds();
        mXCenter = bound.centerX();
        mYCenter = bound.centerY();
        RectF oval = new RectF();
        oval.left = (mXCenter - mRingRadius);
        oval.top = (mYCenter - mRingRadius);
        oval.right = mRingRadius * 2 + (mXCenter - mRingRadius);
        oval.bottom = mRingRadius * 2 + (mYCenter - mRingRadius);
        canvas.drawArc(oval, -90,  360, false, mRingBackgroundPaint); //
//        canvas.drawCircle(mXCenter, mYCenter, mRadius+mStrokeWidth, mRingBackgroundPaint);

    }

    private void drawBar(Canvas canvas, int level, Paint paint) {
        if (level > 0 ) {
            Rect bound= getBounds();
            mXCenter = bound.centerX();
            mYCenter = bound.centerY();
            RectF oval = new RectF();
            oval.left = (mXCenter - mRingRadius);
            oval.top = (mYCenter - mRingRadius);
            oval.right = mRingRadius * 2 + (mXCenter - mRingRadius);
            oval.bottom = mRingRadius * 2 + (mYCenter - mRingRadius);
            canvas.drawArc(oval, -90, ((float) level / mTotalProgress) * 360, false, paint); //
        }
    }

    @Override
    protected boolean onLevelChange(int level) {
        mProgress = level;
//        LogUtils.e("gzq", "mProgress = "+mProgress);
        if(level > 0 && level < mTotalProgress) {
            invalidateSelf();
            return true;
        }else {
            return false;
        }
    }

    @Override
    public void setAlpha(int alpha) {
        mRingPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        mRingPaint.setColorFilter(cf);
    }

    @Override
    public int getOpacity() {
//        return DrawableUtils.getOpacityFromColor(this.mRingPaint.getColor());
//        return Color.TRANSPARENT;
        return PixelFormat.TRANSPARENT;
    }
}
